﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class ManagerWorkRecordController : BaseController<replenishment>
    {
        //
        // GET: /ManagerWorkRecord/
        //RealName  managerCode  storeName   rpsmovertime rpsmId  allPrice
        public class ViewModel
        {
            public string RealName { get; set; }
            public string storeName { get; set; }
            public string rpsmovertime { get; set; }
            public int managerCode { get; set; }
            public string rpsmId { get; set; }
            public decimal allPrice { get; set; }
        }

        public override BLL.BaseBll<replenishment> bll
        {
            get
            {
                return new ReplenishmentBll();
            }

        }
        ManagerBll managerBll = new ManagerBll();

        List<ViewModel> ModelList = new List<ViewModel>();

        public ActionResult Get2(replenishment model,string managerCode, string date1, string date2)      
        {
            decimal all = 0;
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT a.rpsmId,a.managerCode,b.RealName,a.managerCode ,s.storeName ,a.rpsmovertime FROM replenishment as a INNER JOIN manager as b  on a.managerCode=b.userid INNER JOIN store AS s on a.storeId=s.storeid where  rpsmstatus=4  and ");
            //默认情况   默认查询本天的所有记录  
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(managerCode))
            {
                DateTime now = DateTime.Now;
                DateTime beforeWeek = new DateTime(now.Year, now.Month, 1);
                // logTime BETWEEN '2018-07-07 09:31:26' AND '2018-07-21 09:32:10'  GROUP BY waresCode ORDER BY logNum DESC
                sql.Append(" rpsmovertime between  '" + beforeWeek + "' AND '" + now + "'  ORDER BY  rpsmovertime DESC  ");
            }
            //按理货员
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && !string.IsNullOrEmpty(managerCode))
            {
                int code = Convert.ToInt32(managerCode);
                DateTime now = DateTime.Now;
                DateTime beforeWeek = new DateTime(now.Year, now.Month, 1);
                // logTime BETWEEN '2018-07-07 09:31:26' AND '2018-07-21 09:32:10'  GROUP BY waresCode ORDER BY logNum DESC
                sql.Append(" rpsmovertime between  '" + beforeWeek + "' AND '" + now + "' AND managerCode='"+ code+"'   ORDER BY  rpsmovertime DESC  ");
            }
            //按时间
            if (!string.IsNullOrEmpty(date1) && !string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(managerCode))
            {
                DateTime datea = Convert.ToDateTime(date1).Date;
                DateTime dateb = Convert.ToDateTime(date2).AddDays(1);
                // logTime BETWEEN '2018-07-07 09:31:26' AND '2018-07-21 09:32:10'  GROUP BY waresCode ORDER BY logNum DESC
                sql.Append(" rpsmovertime between  '" + datea + "' AND '" + dateb + "'   ORDER BY  rpsmovertime DESC  ");
            }
            //按理货员 和  时间
            if (!string.IsNullOrEmpty(date1) && !string.IsNullOrEmpty(date2) && !string.IsNullOrEmpty(managerCode))
            {
                int code = Convert.ToInt32(managerCode);
                DateTime datea = Convert.ToDateTime(date1).Date;
                DateTime dateb = Convert.ToDateTime(date2).AddDays(1);
                // logTime BETWEEN '2018-07-07 09:31:26' AND '2018-07-21 09:32:10'  GROUP BY waresCode ORDER BY logNum DESC
                sql.Append(" rpsmovertime between  '" + datea + "' AND '" + dateb + "' AND managerCode='" + code + "'   ORDER BY  rpsmovertime DESC  ");
            }
            var list = bll.SearchBySQL<ViewModel>(sql.ToString()).ToList();
            foreach (var item in list)
            {
                //RealName  managerCode  storeName   rpsmovertime rpsmId  allPrice
                ViewModel viewModel = new ViewModel();
                viewModel.rpsmId = item.rpsmId;
                viewModel.managerCode = item.managerCode;
                viewModel.RealName = item.RealName;
                viewModel.storeName = item.storeName;
                viewModel.rpsmovertime = item.rpsmovertime;
                string sql2 = "SELECT SUM(w.waresPrice*a.waresNumb ) as allPrice FROM rpsmdeatils a inner JOIN wares w ON a.waresCode=w.waresCode  WHERE rpsmId='" + viewModel.rpsmId + "'  ";
                var Price = bll.SearchBySQL<ViewModel>(sql2);
                viewModel.allPrice = Price.FirstOrDefault().allPrice;
                all += viewModel.allPrice;
                ModelList.Add(viewModel);
            }
            return Json(new { rows = ModelList, total = ModelList.Count,all=all },JsonRequestBehavior.AllowGet);
        }

         public ActionResult GetManagerName()
        {
            return Json(managerBll.Search(x => x.state == 1).Select(x => new { managerCode = x.userid, managerName = x.RealName }).ToList());
        }
      
    }
}
